﻿using Microsoft.Xna.Framework.Media;

namespace Vixen.Framework
{
    /// <summary>This class encapsulates variables and methods for custom audio handling.</summary>
    public class Audio
    {
        #region State enum

        /// <summary>The playing state of the audio.</summary>
        public enum State
        {
            Stopped,
            Paused,
            Playing
        }

        #endregion

        private readonly Song _audio;
        private State _state;

        /// <summary>Loads an asset in the form of a song.</summary>
        /// <param name="assetName">The name of the asset to load.</param>
        public Audio(string assetName)
        {
            _audio = Engine.LoadAsset<Song>(assetName);
            MediaPlayer.Stop();
            _state = State.Stopped;
        }

        /// <summary>This method plays the audio.</summary>
        public void Play()
        {
            if (_state != State.Playing)
            {
                MediaPlayer.Play(_audio);
                _state = State.Playing;
            }
        }

        /// <summary>This method restarts the audio from the beginning.</summary>
        public void Restart()
        {
            MediaPlayer.Stop();
            MediaPlayer.Play(_audio);
            _state = State.Playing;
        }

        /// <summary>This method stops the audio from playing any further.</summary>
        public void Stop()
        {
            if (_state != State.Stopped)
            {
                MediaPlayer.Stop();
                _state = State.Stopped;
            }
        }
    }
}